﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JuniorProject.Models;
using JuniorProject.Models.Applicant;

namespace JuniorProject.Controllers
{
    public class ApplicantController : Controller
    {
        private OIT_Junior_ProjectEntities db = new OIT_Junior_ProjectEntities();

        // GET: /Applicant/Index
        public ActionResult Index()
        {
            var results = 
                (from q in db.Applications where q.Users.UserName == User.Identity.Name select q).ToList();

            List<ApplicantAppOverviewModel> resultsModels = new List<ApplicantAppOverviewModel>();
            if (results != null)
            {
                resultsModels = results.ConvertAll(new Converter<Applications, ApplicantAppOverviewModel>(DbSearchToAAOMSearch));
            }
            return View(resultsModels);
        }

        #region APPLICATION DETAILS
        // GET: /Applicant/ApplicationDetails_Home
        public ActionResult ApplicationDetails_Home(int id = 0)
        {
            Applications tempApplication = db.Applications.Find(id);
            ApplicantAppOverviewModel tempApplicantModel = new ApplicantAppOverviewModel(tempApplication,
                                                            tempApplication.Users, tempApplication.JobPostings);

            return View(tempApplicantModel);
        }

        // GET: /Applicant/ApplicationDetails_Basic
        public ActionResult ApplicationDetails_Basic(int id = 0)
        {
            ApplicantBasicApplicationModel Abam = BasicApplicationInfoToApplicationModel(db.Applications.Single(x => x.ID == id).Users.BasicApplicationInfo.First());
            ViewData["ApplicationID"] = id;
            return View(Abam);
        }

        // GET: /Applicant/ApplicationDetails_Detailed
        public ActionResult ApplicationDetails_Detailed(int id = 0)
        {
            ApplicantDetailedApplicationModel Adam = DetailedApplicationToDetailedApplicationModel(db.Applications.Single(x => x.ID == id));
            ViewData["ApplicationID"] = id;
            return View(Adam);
        }

        // GET: /Applicant/ApplicationDetails_Questionnaire
        public ActionResult ApplicationDetails_Questionnaire(int id = 0)
        {
            List<UserResponses> tempQuestionnaireList =
                (from q in db.UserResponses where q.ApplicationID == id select q).ToList();
            AQMList AqmList = new AQMList();
            if (tempQuestionnaireList != null)
            {
                AqmList.Questions = tempQuestionnaireList.ConvertAll(new Converter<UserResponses,
                                                ApplicantQuestionnaireModel>(QuestionnaireToModel));
            }
            ViewData["ApplicationID"] = id;
            return View(AqmList);
        }

        // GET: /Applicant/ApplicationDetails_Comments
        public ActionResult ApplicationDetails_Comments(int id = 0)
        {
            List<Comments> tempCommentList =
                (from q in db.Comments where q.ApplicationID == id & q.ApplicantVisible == true select q).ToList();
            ACMList AcmList = new ACMList();
            if (tempCommentList != null)
            {
                AcmList.Comments = tempCommentList.ConvertAll(new Converter<Comments,
                                                ApplicantCommentModel>(CommentsToModel));
            }
            ViewData["ApplicationID"] = id;
            return View(AcmList);
        }
        #endregion

        #region HELPERS
        private ApplicantAppOverviewModel DbSearchToAAOMSearch(Applications db)
        {
            return new ApplicantAppOverviewModel(db, db.Users, db.JobPostings);
        }

        private ApplicantBasicApplicationModel BasicApplicationInfoToApplicationModel(BasicApplicationInfo Bai)
        {
            ApplicantBasicApplicationModel Abam = new ApplicantBasicApplicationModel();
            Abam.AltName = Bai.AltName;
            Abam.Days = Convert.ToBoolean(Bai.Days);
            Abam.Evenings = Convert.ToBoolean(Bai.Evenings);
            Abam.FullTime = Convert.ToBoolean(Bai.FullTime);
            Abam.MonStart = Bai.MonStart;
            Abam.MonStop = Bai.MonStop;
            Abam.TuesStart = Bai.TuesStart;
            Abam.TuesStop = Bai.TuesStop;
            Abam.WedStart = Bai.WedStart;
            Abam.WedStop = Bai.WedStop;
            Abam.ThurStart = Bai.ThurStart;
            Abam.ThurStop = Bai.ThurStop;
            Abam.FriStart = Bai.FriStart;
            Abam.FriStop = Bai.FriStop;
            Abam.SatStart = Bai.SatStart;
            Abam.SatStop = Bai.SatStop;
            Abam.SunStart = Bai.SunStart;
            Abam.SunStop = Bai.SunStop;
            Abam.SSN = Bai.SSN;
            Abam.EmployerName1 = Bai.EmploymentHistory.Name;
            Abam.Position1 = Bai.EmploymentHistory.Position;
            Abam.StartDate1 = Bai.EmploymentHistory.StartDate;
            Abam.EndDate1 = Bai.EmploymentHistory.EndDate;
            Abam.Address11 = Bai.EmploymentHistory.Address1;
            Abam.Address21 = Bai.EmploymentHistory.Address2;
            Abam.City1 = Bai.EmploymentHistory.City;
            Abam.State1 = Bai.EmploymentHistory.State;
            Abam.Zip1 = Bai.EmploymentHistory.Zip;
            Abam.ManagerName1 = Bai.EmploymentHistory.ManagerName;
            Abam.ManagerPhone1 = Bai.EmploymentHistory.Phone;
            Abam.StartSalary1 = Bai.EmploymentHistory.StartSalary;
            Abam.EndSalary1 = Bai.EmploymentHistory.EndSalary;
            Abam.ReasonForLeaving1 = Bai.EmploymentHistory.ReasonForLeaving;
            Abam.Responsibilities1 = Bai.EmploymentHistory.Responsibilities;
            Abam.MayWeContact1 = Convert.ToBoolean(Bai.EmploymentHistory.MayWeContact);
            Abam.SchoolName1 = Bai.Education.SchoolName;
            Abam.SchoolAddress11 = Bai.Education.Address1;
            Abam.SchoolAddress21 = Bai.Education.Address2;
            Abam.SchoolCity1 = Bai.Education.City;
            Abam.SchoolState1 = Bai.Education.State;
            Abam.SchoolZip1 = Bai.Education.Zip;
            Abam.YearStart1 = Bai.Education.YearStart;
            Abam.YearEnd1 = Bai.Education.YearEnd;
            Abam.Graduated1 = Convert.ToBoolean(Bai.Education.Graduated);
            return Abam;
        }

        private ApplicantDetailedApplicationModel DetailedApplicationToDetailedApplicationModel(Applications Dai)
        {
            ApplicantDetailedApplicationModel Adam = new ApplicantDetailedApplicationModel();

            Adam.Address1 = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).Address1;
            Adam.Address2 = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).Address2;
            Adam.ApplicantPhoneNumber1 = Dai.PhoneNumbers.Number;
            if (Dai.PhoneNumbers1 != null)
            {
                Adam.ApplicantPhoneNumber2 = Dai.PhoneNumbers1.Number;
            }
            Adam.City = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).City;
            Adam.DesiredSalary = Dai.Salary;
            Adam.ReferenceCompanyName1 = Dai.References.Company;
            Adam.ReferenceCompanyName2 = Dai.References1.Company;
            Adam.ReferenceCompanyName3 = Dai.References2.Company;
            Adam.ReferenceJobTitle1 = Dai.References.Title;
            Adam.ReferenceJobTitle2 = Dai.References1.Title;
            Adam.ReferenceJobTitle3 = Dai.References2.Title;
            Adam.ReferenceName1 = Dai.References.Name;
            Adam.ReferenceName2 = Dai.References1.Name;
            Adam.ReferenceName3 = Dai.References2.Name;
            Adam.ReferencePhoneNumber1 = Dai.References.Phone;
            Adam.ReferencePhoneNumber2 = Dai.References1.Phone;
            Adam.ReferencePhoneNumber3 = Dai.References2.Phone;
            Adam.State = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).City;
            Adam.Zip = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).Zip;

            return Adam;
        }

        private ApplicantQuestionnaireModel QuestionnaireToModel(UserResponses Responses)
        {
            ApplicantQuestionnaireModel Aqm = new ApplicantQuestionnaireModel();
            Aqm.ApplicationID = Responses.ApplicationID;
            Aqm.ID = Responses.ID;
            Aqm.Question = Responses.Questions.Question;
            Aqm.UserID = Responses.UserID;
            Aqm.Answer = Responses.Answers.Answer;

            return Aqm;
        }

        private ApplicantCommentModel CommentsToModel(Comments Comments)
        {
            ApplicantCommentModel Acm = new ApplicantCommentModel();
            Acm.ApplicationID = Comments.ApplicationID;
            Acm.Comment = Comments.Comment;
            Acm.ApplicantVisible = Comments.ApplicantVisible;
            Acm.Employee = db.Users.Single(x => x.ID == Comments.EmployeeID).UserName;
            Acm.InsertDate = Comments.InsertDate;

            return Acm;
        }
        #endregion

    }
}
